var bearcat = require('bearcat');

var AccountController = function() {
  this.$id = "accountController";
  this.$accountService = null;
}

AccountController.prototype.index0 = function(req, res, next) {
  this.$accountService.getList([0,50], function(err, results){
    if (err) {
      console.log(err);
      return ;
    }

    var r = [];
    console.log(results);
    for (var i = 0; i < results.length; i++) {
      console.log(results[i].toJSON());
      r.push(results[i].toJSON());
    }

    res.render('index0.html',{
      accountList: r
    })
  })
}

AccountController.prototype.new = function(req, res, next) {
  var account_type = req.body.account_type || '';
  account_type = account_type.trim();
  if(!account_type) {
    return res.render('error.html',{
      message:'账户类型是必须的'
    });
  }

  var accountModel = bearcat.getModel("accountModel");
  var r = accountModel.$pack({
    account_name:account_type,
    creat_time:Date.now()
  });

  if (r) {
    console.console.log(r, stack);
    return res.render('error.html',{
      message:r.message
    });
  }

  this.$accountService.addAccount(accountModel, function(err, result){
    if (err) {
      return next(err);
    }
    res.redirect('/index0');
  })
}

module.exports = AccountController;
